/*
 *所有关于upper_ftpmachiningcenter类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*upper_ftpmachiningcenterService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.System.IRepositories;
using System.Collections.Generic;
using System.Reflection.PortableExecutable;
using MathNet.Numerics.Financial;
using System.Text.RegularExpressions;
using static System.Net.WebRequestMethods;
using System;
using VOL.Core.ManageUser;
using NPOI.Util;
using VOL.Entity.VM;

namespace VOL.System.Services
{
    public partial class upper_ftpmachiningcenterService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Iupper_ftpmachiningcenterRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public upper_ftpmachiningcenterService(
            Iupper_ftpmachiningcenterRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="center"></param>
        /// <param name="programs"></param>
        /// <param name="pLAN_WORK_ORDER"></param>
        /// <returns></returns>
        public int CreateModelByExcelNew(upper_ftpmachiningcenter center, List<upper_ftpprogram> programs, PLAN_WORK_ORDER pLAN_WORK_ORDER)
        {
            var iNTE_SHELVES = _repository.DbContext.Set<INTE_SHELVES>().ToList();
            EQU_EQUIPMENT_TOOL eQU_EQUIPMENT_TOOL = new EQU_EQUIPMENT_TOOL();
            //   upper_workpiece workpiece = _repository.DbContext.Set<upper_workpiece>().Where(a => a.workpiece_name == center.workpiece_name).FirstOrDefault();
            PROC_MATERIAL pROC_MATERIAL = _repository.DbContext.Set<PROC_MATERIAL>().Where(a => a.MaterialNameZH == center.workpiece_name).FirstOrDefault();
            if (pROC_MATERIAL == null)
            {
                
                //workpiece = new upper_workpiece();
                //workpiece.workpiece_name = center.workpiece_name;
                string[] arr = center.workpiece_size.Split("×");
                decimal length = 0;
                decimal piecewidth = 0;
                decimal height = 0;
                if (arr.Length == 3)
                {
                    length = decimal.Parse(arr[0]);
                    piecewidth = decimal.Parse(arr[1]);
                    height = decimal.Parse(arr[2]);

                }
                //workpiece.x_instance = 0;
                //workpiece.y_instance = (piecewidth / 2);// 10000;;
                //workpiece.z_instance = 0;
                //workpiece.workpiece_length = length;
                //workpiece.workpiece_width = piecewidth;
                //workpiece.workpiece_height = height;
                EQU_EQUIPMENT eQUIPMENT_No = null;
                if (!string.IsNullOrEmpty(center.processing_machines?.Replace("号机", "")))
                {
                    int id = int.Parse(center.processing_machines?.Replace("号机", ""));
                    //workpiece.machine = _repository.DbContext.Set<upper_machine>().Where(a => a.unit == id).FirstOrDefault().id;
                    eQUIPMENT_No= _repository.DbContext.Set<EQU_EQUIPMENT>().Where(a => a.unit == id).FirstOrDefault();
                    eQU_EQUIPMENT_TOOL.EquipmentId = eQUIPMENT_No.Id.ToString().ToUpper();
                }
                else
                {
                    //workpiece.machine = _repository.DbContext.Set<upper_machine>().Where(a => a.unit == 1).FirstOrDefault().id;
                    eQUIPMENT_No = _repository.DbContext.Set<EQU_EQUIPMENT>().Where(a => a.unit == 1).FirstOrDefault();
                    eQU_EQUIPMENT_TOOL.EquipmentId = eQUIPMENT_No.Id.ToString().ToUpper();
                }
                //                               

                //创建工件 产品
                pROC_MATERIAL=new PROC_MATERIAL();
                pROC_MATERIAL.target_accuracy=decimal.Parse( pLAN_WORK_ORDER.process_precision);//精度
                pROC_MATERIAL.Hardness = pLAN_WORK_ORDER.MaterialHardness;
                pROC_MATERIAL.Img= pLAN_WORK_ORDER.Img;
                pROC_MATERIAL.Identifier = center.mold_number;
                pROC_MATERIAL.MaterialNameZH = center.workpiece_name;
                pROC_MATERIAL.workpiece_name = center.workpiece_name;
                pROC_MATERIAL.WorkpieceCode=center.workpiece_name;
                if (arr.Length == 3)
                {
                    length = decimal.Parse(arr[0]);
                    piecewidth = decimal.Parse(arr[1]);
                    height = decimal.Parse(arr[2]);

                }
                pROC_MATERIAL.x_instance = 0;
                pROC_MATERIAL.y_instance = (piecewidth / 2);// 10000;
                pROC_MATERIAL.z_instance = 0;
                pROC_MATERIAL.workpiece_length = length;
                pROC_MATERIAL.workpiece_width = piecewidth;
                pROC_MATERIAL.workpiece_height = height;
                pROC_MATERIAL.MaterialType = "1";
                pROC_MATERIAL.CreateBy = UserContext.Current.UserName;
                pROC_MATERIAL.CreateOn = DateTime.Now;
                pROC_MATERIAL.Weight = pLAN_WORK_ORDER.ProcessWeight; 
                if (!string.IsNullOrEmpty(center.processing_machines?.Replace("号机", "")))
                {
                    int id = int.Parse(center.processing_machines?.Replace("号机", ""));
                    pROC_MATERIAL.machine = _repository.DbContext.Set<upper_machine>().Where(a => a.unit == id).FirstOrDefault().id;
                }
                else
                {
                    pROC_MATERIAL.machine = _repository.DbContext.Set<upper_machine>().Where(a => a.unit == 1).FirstOrDefault().id;
                }
                //PROC_CALL_MATERIAL
                PROC_CALL_MATERIAL pROC_CALL_MATERIAL = new PROC_CALL_MATERIAL();
               
                pROC_CALL_MATERIAL.CreateBy = UserContext.Current.UserName;
                pROC_CALL_MATERIAL.CreateOn = DateTime.Now;
                var countTotalPlanOrder = _repository.DbContext.Set<PLAN_WORK_ORDER>().Count();
                using (var transaction = _repository.DbContext.Database.BeginTransaction())
                {
                    try
                    {
                        //if (workpiece.id == 0)
                        //{
                           // _repository.DbContext.Set<upper_workpiece>().Add(workpiece);
                            _repository.DbContext.Set<PROC_MATERIAL>().Add(pROC_MATERIAL);
                            
                            _repository.DbContext.SaveChanges();
                        //_repository.DbContext.Entry<upper_workpiece>(workpiece);
                        //}

                        var workpiece_id = pROC_MATERIAL.workpiece_id;// workpiece.id;

                        if (_repository.DbContext.Set<PLAN_WORK_ORDER>().Any(a => a.work_order_number == center.mold_number))
                        {
                            transaction.Rollback();
                            return 0;
                        }

                        //创建工单
                    //    upper_work_order work_Order = new upper_work_order();
                        //work_Order.workpiece_id = workpiece_id;
                        //work_Order.work_order_number = center.mold_number;
                        //work_Order.height = height;
                        //work_Order.width = piecewidth;
                        //work_Order.length = length;

              

                        //work_Order.material_id = 1;
                        //work_Order.priority = 1;
                        //work_Order.processing_grade = 1;
                        //work_Order.accuracy = 0.02m;
                        //work_Order.measure_number = pLAN_WORK_ORDER.measure_number;
                        //work_Order.centring_number = pLAN_WORK_ORDER.centring_number;
                        //work_Order.make_level_number = pLAN_WORK_ORDER.make_level_number;
                        //_repository.DbContext.Set<upper_work_order>().Add(work_Order);
                        //_repository.DbContext.SaveChanges();

                        PLAN_WORK_ORDER planWorkOrder = new PLAN_WORK_ORDER();
                        planWorkOrder.accuracy= decimal.Parse(pLAN_WORK_ORDER.process_precision);//精度
                        planWorkOrder.UsedToolTotal = programs.Count();
                        planWorkOrder.OrderType = pLAN_WORK_ORDER.OrderType;
                        planWorkOrder.Img = pLAN_WORK_ORDER.Img;
                        planWorkOrder.Version = pLAN_WORK_ORDER.Version;
                        planWorkOrder.Status = ((int)StatusOrder.New).ToString();//0新建 
                        planWorkOrder.process_Status = ((int)StatusProcess.NotStart);
                        planWorkOrder.ProductId = pROC_MATERIAL.Id.ToString().ToUpper();
                        planWorkOrder.OrderCode = DateTimeHelper.CreateOrderNo(center.mold_number, countTotalPlanOrder+1);                   
                        planWorkOrder.workpiece_id = workpiece_id;
                        planWorkOrder.work_order_number = center.mold_number;
                        planWorkOrder.height = height;
                        planWorkOrder.width = piecewidth;
                        planWorkOrder.length = length;
                        planWorkOrder.material_id = 1;
                        planWorkOrder.priority = 1;
                        planWorkOrder.processing_grade = 1;
                        planWorkOrder.accuracy = 0.02m;
                        planWorkOrder.CreateBy = UserContext.Current.UserName; // UserContext.Current.UserName;
                        planWorkOrder.CreateOn = DateTime.Now;
                        planWorkOrder.TollHardness= pLAN_WORK_ORDER.TollHardness;
                        planWorkOrder.MaterialHardness= pLAN_WORK_ORDER.MaterialHardness;
                        //程序名 1打表,2分中,3放平
                        planWorkOrder.measure_number = pLAN_WORK_ORDER.measure_number;
                        planWorkOrder.centring_number = pLAN_WORK_ORDER.centring_number;
                        planWorkOrder.make_level_number = pLAN_WORK_ORDER.make_level_number;
                        planWorkOrder.PlanStart = pLAN_WORK_ORDER.PlanStart;
                        planWorkOrder.PlanEnd = pLAN_WORK_ORDER.PlanEnd;
                        planWorkOrder.OrderQuantity = 1;
                        planWorkOrder.processing_machines = center.processing_machines;
                        planWorkOrder.process_type = pLAN_WORK_ORDER.process_type;
                        planWorkOrder.ProcessWeight = pLAN_WORK_ORDER.ProcessWeight;
                        planWorkOrder.ProcessingMaterial = pLAN_WORK_ORDER.ProcessingMaterial;
                        planWorkOrder.process_precision= pLAN_WORK_ORDER.process_precision;
                        planWorkOrder.HolderType = pLAN_WORK_ORDER.HolderType;
                        planWorkOrder.RotationalSpeed = pLAN_WORK_ORDER.RotationalSpeed;

                        _repository.DbContext.Set<PLAN_WORK_ORDER>().Add(planWorkOrder);
                        _repository.DbContext.SaveChanges();
                        // _repository.DbContext.Entry<upper_work_order>(work_Order);
                        //var workOrderId = 0;// work_Order.work_order_id;
                        var proCodeMain = planWorkOrder.OrderCode;//程序编号

                        center.work_order_id = planWorkOrder.work_order_id;//workOrderId;
                        _repository.DbContext.Entry(center).State = EntityState.Added;
                        _repository.SaveChanges();

                        pROC_MATERIAL.Order_Id= planWorkOrder.Order_Id.ToString().ToUpper();
                        _repository.DbContext.Entry(pROC_MATERIAL).State = EntityState.Modified;
                        _repository.SaveChanges();

                        eQU_EQUIPMENT_TOOL.ProCode = proCodeMain;
                        eQU_EQUIPMENT_TOOL.CreateBy = UserContext.Current.UserName;
                        eQU_EQUIPMENT_TOOL.CreateOn = DateTime.Now;
                        _repository.DbContext.Set<EQU_EQUIPMENT_TOOL>().Add(eQU_EQUIPMENT_TOOL);
                        // _repository.DbContext.Entry(center);
                        var ftpId = center.ftp_id;

                        pROC_CALL_MATERIAL.Order_Id =planWorkOrder.Order_Id.ToString().ToUpper();
                        pROC_CALL_MATERIAL.Img =planWorkOrder.Img;
                        pROC_CALL_MATERIAL.OrderQuantity=planWorkOrder.OrderQuantity;
                        pROC_CALL_MATERIAL.WorkpieceCode= center.workpiece_name;
                        pROC_CALL_MATERIAL.StorageState = "未入库";
                        pROC_CALL_MATERIAL.NeedState = "正常装料";
                        _repository.DbContext.Set<PROC_CALL_MATERIAL>().Add(pROC_CALL_MATERIAL);
                        foreach (var item in programs)
                        {
                            item.ftpmachiningcenter_id = ftpId;
                            //item.accuracy = item.category == "光" ? 0.03m : 0.1m;
                            _repository.DbContext.Entry(item).State = EntityState.Added;

                           // //同时生成刀具
                           //// upper_tool tool = new upper_tool();
                           // tool.work_order_id = workOrderId;
                           // tool.location = item.location;
                           // tool.tool_name = item.tool_name;
                           // tool.handle_type = item.tool_handle;
                           // tool.collet_type = item.tool_collet;
                           // //tool.R=
                           // tool.knife_length = item.knife_length;
                           // tool.blade_length = item.blade_length;
                           // tool.category = item.category;
                           // tool.handle_type = item.tool_handle;
                           // tool.collet_type = item.tool_collet;
                           
                           // tool.reality_pairing_length = item.knife_length;
                            
                           // tool.vendor_code = item.vendor_code;
                           // tool.tool_materials = item.tool_materials;
                            //tool.collet_length=
                            // tool.extensions_type=
                            //tool.extensions_length=
                            //tool.tool_type=
                            // tool.tool_length= tool.knife_length;
                            //tool.pairing_length=
                            // tool.blade_width= 
                            string input = item.tool_name;
                            Regex regex = new Regex(@"[0-9]+(\.[0-9]+)?");
                            MatchCollection matches = regex.Matches(input);
                            List<decimal> numbers = new List<decimal>();
                            foreach (Match match in matches)
                            {
                                if (decimal.TryParse(match.Value, out decimal result))
                                {
                                    // 将转换成功的数字添加到 List 中
                                    numbers.Add(result);
                                }
                            }

                            decimal width = numbers[0];
                            decimal r=0;
                            if (numbers.Count >1)
                            {
                                r = numbers[1];
                            }

                            //R角检查
                            if (!item.tool_name.Contains("Ø")|| !item.tool_name.Contains("R"))
                            {
                                transaction.Rollback();
                                return -2;

                            }
                            if (r> eQUIPMENT_No.Max_reality_R)
                            {
                                transaction.Rollback();
                                return -3;

                            }
                           
                            
                            //tool.knife_width = width;
                            //tool.reality_knife_width = width;
                            //tool.R = r;
                            //tool.reality_R = r;
                            //tool.accuracy = item.accuracy;
                            //tool.program_seq = (int?)int.Parse(item.sequence);
                            //tool.work_order_status = planWorkOrder.process_Status;
                            //_repository.DbContext.Entry(tool).State = EntityState.Added;

                            EQU_TOOL eQU_TOOL = new EQU_TOOL();
                            eQU_TOOL.work_order_id = planWorkOrder.work_order_id;//workOrderId;
                            var iNTE_SHELVESFirst= iNTE_SHELVES.Where(t => t.DBCode == item.location).FirstOrDefault();
                            if (iNTE_SHELVESFirst != null)
                            {
                                eQU_TOOL.SHELVES_Id = iNTE_SHELVESFirst.INTE_SHELVES_Id.ToString().ToUpper();
                                eQU_TOOL.location= iNTE_SHELVESFirst.DBCode;
                            }
                            eQU_TOOL.tool_name = item.tool_name;
                            eQU_TOOL.handle_type = item.tool_handle;
                            eQU_TOOL.collet_type = item.tool_collet;
                            //tool.R=
                            eQU_TOOL.knife_length = item.knife_length;
                            eQU_TOOL.blade_length = item.blade_length;
                            eQU_TOOL.category = item.category;
                            eQU_TOOL.handle_type = item.tool_handle;
                            eQU_TOOL.collet_type = item.tool_collet;
                            
                            eQU_TOOL.reality_pairing_length = 0;
                            eQU_TOOL.reality_knife_length = item.knife_length;
                           
                            eQU_TOOL.vendor_code = item.vendor_code;
                            eQU_TOOL.tool_materials = item.tool_materials;
                            eQU_TOOL.knife_width = width;
                            eQU_TOOL.reality_knife_width = width;// item.knife_width;
                            eQU_TOOL.R = r;
                            eQU_TOOL.reality_R = r;
                            eQU_TOOL.time = item.time;
                            eQU_TOOL.accuracy = item.accuracy;
                            eQU_TOOL.program_seq = (int?)int.Parse(item.sequence);
                            eQU_TOOL.work_order_status = planWorkOrder.process_Status;
                            eQU_TOOL.CreateBy = UserContext.Current.UserName;
                            eQU_TOOL.CreateOn = DateTime.Now;
                            eQU_TOOL.Identifier = proCodeMain;
                            eQU_TOOL.Ace1LTheory = item.Ace1LTheory;
                            eQU_TOOL.Ace1DTheory = item.Ace1DTheory;
                            eQU_TOOL.Ace2LTheory = item.Ace2LTheory;
                            eQU_TOOL.Ace2DTheory = item.Ace2DTheory;
                            eQU_TOOL.Ace3LTheory = item.Ace3LTheory;
                            eQU_TOOL.Ace3DTheory = item.Ace3DTheory;
                            eQU_TOOL.Holder_Code = item.Holder_Code;
                            eQU_TOOL.Hardness= pLAN_WORK_ORDER.TollHardness;
                            eQU_TOOL.HolderTypeId = pLAN_WORK_ORDER.HolderType;// eQUIPMENT_No.HolderTypeId;
                            _repository.DbContext.Entry(eQU_TOOL).State = EntityState.Added;

                            PROC_PROGRAM pROC_PROGRAM = new PROC_PROGRAM();                            
                            pROC_PROGRAM.ParentId="0";
                            pROC_PROGRAM.UseState= "未使用";// "1:未使用 2:使用中 3:已使用"
                            pROC_PROGRAM.ftpmachiningcenter_id = item.ftpmachiningcenter_id;
                            pROC_PROGRAM.sequence = int.Parse(item.sequence);
                            pROC_PROGRAM.program_name = item.program_name;
                            pROC_PROGRAM.Identifier = proCodeMain;
                            pROC_PROGRAM.tool_name = item.tool_name;
                            pROC_PROGRAM.tool_handle = item.tool_handle;
                            pROC_PROGRAM.tool_collet = item.tool_collet;
                            pROC_PROGRAM.knife_length = item.knife_length;
                            pROC_PROGRAM.blade_length = item.blade_length;
                            pROC_PROGRAM.category = item.category;
                            pROC_PROGRAM.accuracy = item.accuracy;
                            pROC_PROGRAM.rotate_speed = item.rotate_speed;
                            pROC_PROGRAM.feed = item.feed;
                            pROC_PROGRAM.side_bottom_margin = item.side_bottom_margin;
                            pROC_PROGRAM.z_depth = item.z_depth;
                            pROC_PROGRAM.tool_materials = item.tool_materials;
                            pROC_PROGRAM.vendor_code = item.vendor_code;
                            pROC_PROGRAM.remark = item.remark;
                            pROC_PROGRAM.time = item.time;
                            pROC_PROGRAM.CreateBy = UserContext.Current.UserName;
                            pROC_PROGRAM.CreateOn = DateTime.Now;
                            pROC_PROGRAM.ProCode = proCodeMain;
                            pROC_PROGRAM.Id = Guid.Parse(Guid.NewGuid().ToString().ToUpper());

                            _repository.DbContext.Entry(pROC_PROGRAM).State = EntityState.Added;

                            PROC_MATERIAL_TOOL pROC_MATERIAL_TOOL = new PROC_MATERIAL_TOOL();
                            pROC_MATERIAL_TOOL.MaterialId = pROC_MATERIAL.Id.ToString().ToUpper();
                            pROC_MATERIAL_TOOL.ToolId = eQU_TOOL.Id.ToString().ToUpper();
                            pROC_MATERIAL_TOOL.ProgramId = pROC_PROGRAM.Id.ToString().ToUpper();
                            pROC_MATERIAL_TOOL.CreateBy = UserContext.Current.UserName;
                            pROC_MATERIAL_TOOL.CreateOn = DateTime.Now;
                            _repository.DbContext.Entry(pROC_MATERIAL_TOOL).State = EntityState.Added;

                            EQU_TOOL_PROGAME eQU_TOOL_PROGAME = new EQU_TOOL_PROGAME();
                            eQU_TOOL_PROGAME.OrderCode = proCodeMain;
                            eQU_TOOL_PROGAME.ToolId = eQU_TOOL.Id.ToString().ToUpper();
                            eQU_TOOL_PROGAME.ProgrameId = pROC_PROGRAM.Id.ToString().ToUpper();
                            eQU_TOOL_PROGAME.ExecuteSort = 1;
                            eQU_TOOL_PROGAME.CreateBy = UserContext.Current.UserName;
                            eQU_TOOL_PROGAME.CreateOn = DateTime.Now;

                            _repository.DbContext.Entry(eQU_TOOL_PROGAME).State = EntityState.Added;
                        }
                        _repository.SaveChanges();
                        transaction.Commit();

                        return 1;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        return -1;
                    }
                }
            }
            else
            {
                using (var transaction = _repository.DbContext.Database.BeginTransaction())
                {
                    //只添加item tool
                    //  var workorder = _repository.DbContext.Set<upper_work_order>().Where(a => a.workpiece_id == workpiece.id).FirstOrDefault();
                    PLAN_WORK_ORDER planWorkOrder = _repository.DbContext.Set<PLAN_WORK_ORDER>().Where(a => a.work_order_number == center.mold_number).FirstOrDefault();
                    // var workorder = _repository.DbContext.Set<upper_work_order>().Where(a => a.workpiece_id == planWorkOrder.workpiece_id).FirstOrDefault();
                    planWorkOrder.accuracy=decimal.Parse(pLAN_WORK_ORDER.process_precision);//精度
                    var workorderid = planWorkOrder.work_order_id;
                    upper_ftpmachiningcenter machiningcenter = _repository.DbContext.Set<upper_ftpmachiningcenter>().Where(a => a.work_order_id == workorderid).FirstOrDefault();
                    var machiningprograms = _repository.DbContext.Set<upper_ftpprogram>().Where(a => a.ftpmachiningcenter_id == machiningcenter.ftp_id).ToList();

                    //更新刀具time by guoyuguo 2024.9.24
                    planWorkOrder.ProcessingMaterial = pLAN_WORK_ORDER.ProcessingMaterial;
                    planWorkOrder.process_precision = pLAN_WORK_ORDER.process_precision;
                    planWorkOrder.HolderType = pLAN_WORK_ORDER.HolderType;
                    planWorkOrder.ProcessWeight = pLAN_WORK_ORDER.ProcessWeight;
                    planWorkOrder.RotationalSpeed = pLAN_WORK_ORDER.RotationalSpeed;
                    planWorkOrder.Img = pLAN_WORK_ORDER.Img;
                    planWorkOrder.UsedToolTotal = programs.Count();
                    _repository.DbContext.Entry(planWorkOrder).State = EntityState.Modified;
                    var orerid = Guid.Parse(planWorkOrder.ProductId.ToString());
                    PROC_MATERIAL procMaterial = _repository.DbContext.Set<PROC_MATERIAL>().Where(a => a.Id == orerid).FirstOrDefault();
                    procMaterial.Img = pLAN_WORK_ORDER.Img;
                    procMaterial.Weight = pLAN_WORK_ORDER.ProcessWeight;
                    _repository.DbContext.Entry(procMaterial).State = EntityState.Modified;
                    var equ = _repository.DbContext.Set<EQU_EQUIPMENT>().Where(t => t.EquipmentNo == procMaterial.machine.ToString()).FirstOrDefault();
                    foreach (var item in programs)
                    {

                        var sequence = int.Parse(item.sequence);
                        PROC_PROGRAM pROC_PROGRAM = _repository.DbContext.Set<PROC_PROGRAM>().Where(a => a.Identifier.StartsWith(planWorkOrder.OrderCode) && a.sequence == sequence).FirstOrDefault();
                        if (pROC_PROGRAM != null)
                        {
                            pROC_PROGRAM.time = item.time;
                            _repository.DbContext.Entry(pROC_PROGRAM).State = EntityState.Modified;

                        }
                        if (machiningprograms.Any(a => a.program_name == item.program_name))
                        {
                            string input = item.tool_name;
                            //R角检查
                            if (!item.tool_name.Contains("Ø") ||!item.tool_name.Contains("R"))
                            {
                                transaction.Rollback();
                                return -2;

                            }
                       
                            Regex regex = new Regex(@"[0-9]+(\.[0-9]+)?");
                            MatchCollection matches = regex.Matches(input);
                            List<decimal> numbers = new List<decimal>();
                            foreach (Match match in matches)
                            {
                                if (decimal.TryParse(match.Value, out decimal result))
                                {
                                    // 将转换成功的数字添加到 List 中
                                    numbers.Add(result);
                                }
                            }

                            decimal width = numbers[0];
                            decimal r = numbers[1];
                            var proseq = (int?)int.Parse(item.sequence);
                            EQU_TOOL eQU_TOOL = _repository.DbContext.Set<EQU_TOOL>().Where(a => a.Identifier.StartsWith(planWorkOrder.OrderCode) && a.program_seq == proseq).FirstOrDefault();
                            if (eQU_TOOL != null)
                            {
                                eQU_TOOL.time = item.time;
                                //   if(eQU_TOOL.reality_pairing_length==0)//刀具长度
                                eQU_TOOL.reality_pairing_length = 0;//item.knife_length;
                                eQU_TOOL.reality_R = r;
                                eQU_TOOL.reality_knife_width = width;
                                eQU_TOOL.accuracy = item.accuracy;
                                if (equ != null)
                                {
                                    eQU_TOOL.HolderTypeId = pLAN_WORK_ORDER.HolderType; //equ.HolderTypeId;
                                }
                                var iNTE_SHELVESFirst = iNTE_SHELVES.Where(t => t.DBCode == item.location).FirstOrDefault();
                                if (iNTE_SHELVESFirst != null)
                                {
                                    eQU_TOOL.SHELVES_Id = iNTE_SHELVESFirst.INTE_SHELVES_Id.ToString().ToUpper();
                                    eQU_TOOL.location = iNTE_SHELVESFirst.DBCode;
                                }
                                _repository.DbContext.Entry(eQU_TOOL).State = EntityState.Modified;

                            }
                            continue;
                        }
                        else
                        {
                            item.ftpmachiningcenter_id = machiningcenter.ftp_id;
                            //item.accuracy = item.category == "光" ? 0.03m : 0.1m;
                            _repository.DbContext.Entry(item).State = EntityState.Added;

                            ////同时生成刀具
                            //upper_tool tool = new upper_tool();
                            //tool.work_order_id = workorderid;
                            //tool.location = item.location;
                            //tool.tool_name = item.tool_name;
                            //tool.handle_type = item.tool_handle;
                            //tool.collet_type = item.tool_collet;
                            ////tool.R=
                            //tool.knife_length = item.knife_length;
                            //tool.blade_length = item.blade_length;
                            //tool.category = item.category;
                            //tool.handle_type = item.tool_handle;
                            //tool.collet_type = item.tool_collet;



                            //tool.collet_length=
                            // tool.extensions_type=
                            //tool.extensions_length=
                            //tool.tool_type=
                            // tool.tool_length= tool.knife_length;
                            //tool.pairing_length=
                            // tool.blade_width= 
                            string input = item.tool_name;
                            //R角检查
                            if (!item.tool_name.Contains("Ø") || !item.tool_name.Contains("R"))
                            {
                                transaction.Rollback();
                                return -2;

                            }
                         
                            Regex regex = new Regex(@"[0-9]+(\.[0-9]+)?");
                            MatchCollection matches = regex.Matches(input);
                            List<decimal> numbers = new List<decimal>();
                            foreach (Match match in matches)
                            {
                                if (decimal.TryParse(match.Value, out decimal result))
                                {
                                    // 将转换成功的数字添加到 List 中
                                    numbers.Add(result);
                                }
                            }

                            decimal width = numbers[0];
                            decimal r = numbers[1];
                            //tool.reality_knife_width = width;
                            //tool.reality_pairing_length = item.knife_length;
                            //tool.knife_width = width;
                            //tool.R = r;
                            //tool.reality_R = r;
                            //tool.accuracy = item.accuracy;
                            //tool.program_seq = (int?)int.Parse(item.sequence);
                            //tool.work_order_status = workorder.process_Status;
                            //_repository.DbContext.Entry(tool).State = EntityState.Added;

                            var proseq = (int?)int.Parse(item.sequence);
                            EQU_TOOL eQU_TOOL = _repository.DbContext.Set<EQU_TOOL>().Where(a => a.Identifier.StartsWith(planWorkOrder.OrderCode) && a.program_seq == proseq).FirstOrDefault();
                            if (eQU_TOOL != null)
                            {
                                eQU_TOOL.time = item.time;
                                eQU_TOOL.reality_pairing_length = item.knife_length;
                                eQU_TOOL.reality_knife_length = item.knife_length;
                                eQU_TOOL.reality_R = r;
                                eQU_TOOL.reality_knife_width = width;
                                eQU_TOOL.accuracy = item.accuracy;
                                var iNTE_SHELVESFirst = iNTE_SHELVES.Where(t => t.DBCode == item.location).FirstOrDefault();
                                if (iNTE_SHELVESFirst != null)
                                {
                                    eQU_TOOL.SHELVES_Id = iNTE_SHELVESFirst.INTE_SHELVES_Id.ToString().ToUpper();
                                    eQU_TOOL.location = iNTE_SHELVESFirst.DBCode;
                                }
                                _repository.DbContext.Entry(eQU_TOOL).State = EntityState.Modified;

                            }

                        }

                    }
                    _repository.SaveChanges();
                    transaction.Commit();
                    return 1;
                }
            }


        }

        public int CreateModelByExcel(upper_ftpmachiningcenter center,List<upper_ftpprogram> programs,PLAN_WORK_ORDER pLAN_WORK_ORDER)
        {
            EQU_EQUIPMENT_TOOL eQU_EQUIPMENT_TOOL = new EQU_EQUIPMENT_TOOL();
            upper_workpiece workpiece = _repository.DbContext.Set<upper_workpiece>().Where(a => a.workpiece_name == center.workpiece_name).FirstOrDefault();
            var countTotalPlanOrder = _repository.DbContext.Set<PLAN_WORK_ORDER>().Count();
            if (workpiece == null)
            {
                workpiece = new upper_workpiece();
                workpiece.workpiece_name = center.workpiece_name;
                string[] arr = center.workpiece_size.Split("×");
                decimal length = 0;
                decimal piecewidth = 0;
                decimal height = 0;
                if (arr.Length == 3)
                {
                    length = decimal.Parse(arr[0]);
                    piecewidth = decimal.Parse(arr[1]);
                    height = decimal.Parse(arr[2]);

                }
                workpiece.x_instance = 0;
                workpiece.y_instance = (pLAN_WORK_ORDER.width.Value / 2);// 10000;
                workpiece.z_instance = 0;
                workpiece.workpiece_length = length;
                workpiece.workpiece_width = piecewidth;
                workpiece.workpiece_height = height;
                if (!string.IsNullOrEmpty(center.processing_machines?.Replace("号机", "")))
                {
                    int id = int.Parse(center.processing_machines?.Replace("号机", ""));
                    workpiece.machine = _repository.DbContext.Set<upper_machine>().Where(a => a.unit == id).FirstOrDefault().id;
                    eQU_EQUIPMENT_TOOL.EquipmentId= _repository.DbContext.Set<EQU_EQUIPMENT>().Where(a => a.unit == id).FirstOrDefault().Id.ToString().ToUpper();
                }
                else
                {
                    workpiece.machine = _repository.DbContext.Set<upper_machine>().Where(a => a.unit == 1).FirstOrDefault().id;
                    eQU_EQUIPMENT_TOOL.EquipmentId = _repository.DbContext.Set<EQU_EQUIPMENT>().Where(a => a.unit == 1).FirstOrDefault().Id.ToString().ToUpper();
                }
                //                               
               
                //创建工件 产品
                PROC_MATERIAL pROC_MATERIAL = new PROC_MATERIAL();
                pROC_MATERIAL.Identifier =center.mold_number;
                pROC_MATERIAL.MaterialNameZH = center.workpiece_name;
                pROC_MATERIAL.workpiece_name = center.workpiece_name;
                if (arr.Length == 3)
                {
                    length = decimal.Parse(arr[0]);
                    piecewidth = decimal.Parse(arr[1]);
                    height = decimal.Parse(arr[2]);

                }
                pROC_MATERIAL.x_instance = 0;
                pROC_MATERIAL.y_instance = (pLAN_WORK_ORDER.width.Value / 2);// 10000;
                pROC_MATERIAL.z_instance = 0;
                pROC_MATERIAL.workpiece_length = length;
                pROC_MATERIAL.workpiece_width = piecewidth;
                pROC_MATERIAL.workpiece_height = height;
                pROC_MATERIAL.MaterialType = "1";
                pROC_MATERIAL.CreateBy = UserContext.Current.UserName;
                pROC_MATERIAL.CreateOn = DateTime.Now;
                if (!string.IsNullOrEmpty(center.processing_machines?.Replace("号机", "")))
                {
                    int id = int.Parse(center.processing_machines?.Replace("号机", ""));
                    pROC_MATERIAL.machine = _repository.DbContext.Set<upper_machine>().Where(a => a.unit == id).FirstOrDefault().id;
                }
                else
                {
                    pROC_MATERIAL.machine = _repository.DbContext.Set<upper_machine>().Where(a => a.unit == 1).FirstOrDefault().id;
                }

                using (var transaction = _repository.DbContext.Database.BeginTransaction())
                {
                    try
                    {
                        if (workpiece.id == 0)
                        {
                            _repository.DbContext.Set<upper_workpiece>().Add(workpiece);
                            _repository.DbContext.Set<PROC_MATERIAL>().Add(pROC_MATERIAL);

                            _repository.DbContext.SaveChanges();
                            _repository.DbContext.Entry<upper_workpiece>(workpiece);
                        }

                        var workpiece_id = workpiece.id;

                        if (_repository.DbContext.Set<upper_work_order>().Any(a => a.work_order_number == center.mold_number))
                        {
                            transaction.Rollback();
                            return 0;
                        }
                      
                        //创建工单
                        upper_work_order work_Order = new upper_work_order();
                        work_Order.workpiece_id = workpiece_id;
                        work_Order.work_order_number = center.mold_number;
                        work_Order.height = workpiece.workpiece_height;
                        work_Order.width = workpiece.workpiece_width;
                        work_Order.length = workpiece.workpiece_length;
                        work_Order.material_id = 1;
                        work_Order.priority = 1;
                        work_Order.processing_grade = 1;
                        work_Order.accuracy = 0.02m;
                        work_Order.measure_number = pLAN_WORK_ORDER.measure_number;
                        work_Order.centring_number = pLAN_WORK_ORDER.centring_number;
                        work_Order.make_level_number = pLAN_WORK_ORDER.make_level_number;
                        _repository.DbContext.Set<upper_work_order>().Add(work_Order);
                        _repository.DbContext.SaveChanges();

                        PLAN_WORK_ORDER planWorkOrder = new PLAN_WORK_ORDER();
                        planWorkOrder.UsedToolTotal = programs.Count();
                        planWorkOrder.ProductId= pROC_MATERIAL.Id.ToString().ToUpper();
                        planWorkOrder.OrderCode= DateTimeHelper.CreateOrderNo(center.mold_number, countTotalPlanOrder);
                        planWorkOrder.workpiece_id = workpiece_id;
                        planWorkOrder.work_order_number = center.mold_number;
                        planWorkOrder.height = workpiece.workpiece_height;
                        planWorkOrder.width = workpiece.workpiece_width;
                        planWorkOrder.length = workpiece.workpiece_length;
                        planWorkOrder.material_id = 1;
                        planWorkOrder.priority = 1;
                        planWorkOrder.processing_grade = 1;
                        planWorkOrder.accuracy = 0.02m;
                        planWorkOrder.CreateBy = UserContext.Current.UserName; // UserContext.Current.UserName;
                        planWorkOrder.CreateOn = DateTime.Now;
                        //程序名 1打表,2分中,3放平
                        planWorkOrder.measure_number = pLAN_WORK_ORDER.measure_number;
                        planWorkOrder.centring_number = pLAN_WORK_ORDER.centring_number;
                        planWorkOrder.make_level_number= pLAN_WORK_ORDER.make_level_number;
                        planWorkOrder.PlanStart=pLAN_WORK_ORDER.PlanStart;
                        planWorkOrder.PlanEnd = pLAN_WORK_ORDER.PlanEnd;
                        planWorkOrder.OrderQuantity = 1;
                        planWorkOrder.processing_machines = center.processing_machines;
                        planWorkOrder.process_type = pLAN_WORK_ORDER.process_type;
                        planWorkOrder.ProcessWeight= pLAN_WORK_ORDER.ProcessWeight;
                        _repository.DbContext.Set<PLAN_WORK_ORDER>().Add(planWorkOrder);
                        //_repository.DbContext.SaveChanges();
                        // _repository.DbContext.Entry<upper_work_order>(work_Order);
                        var workOrderId = work_Order.work_order_id;
                        var proCodeMain = planWorkOrder.OrderCode;//程序编号

                        center.work_order_id = workOrderId;
                        _repository.DbContext.Entry(center).State = EntityState.Added;
                        _repository.SaveChanges();

                        eQU_EQUIPMENT_TOOL.ProCode = proCodeMain;
                        eQU_EQUIPMENT_TOOL.CreateBy = UserContext.Current.UserName;
                        eQU_EQUIPMENT_TOOL.CreateOn = DateTime.Now;
                        _repository.DbContext.Set<EQU_EQUIPMENT_TOOL>().Add(eQU_EQUIPMENT_TOOL);
                        // _repository.DbContext.Entry(center);
                        var ftpId = center.ftp_id;
                    
                        foreach (var item in programs)
                        {                                                                               
                            item.ftpmachiningcenter_id = ftpId;
                            //item.accuracy = item.category == "光" ? 0.03m : 0.1m;
                            _repository.DbContext.Entry(item).State = EntityState.Added;

                            //同时生成刀具
                            upper_tool tool = new upper_tool();
                            tool.work_order_id = workOrderId;

                            tool.tool_name = item.tool_name;
                            tool.handle_type = item.tool_handle;
                            tool.collet_type = item.tool_collet;
                            //tool.R=
                            tool.knife_length = item.knife_length;
                            tool.blade_length = item.blade_length;
                            tool.category = item.category;
                            tool.handle_type = item.tool_handle;
                            tool.collet_type = item.tool_collet;
                            tool.reality_knife_width = 10000;
                            tool.reality_pairing_length = 10000;
                            tool.reality_R = 10000;
                            tool.vendor_code = item.vendor_code;
                            tool.tool_materials = item.tool_materials;
                            //tool.collet_length=
                            // tool.extensions_type=
                            //tool.extensions_length=
                            //tool.tool_type=
                            // tool.tool_length= tool.knife_length;
                            //tool.pairing_length=
                            // tool.blade_width= 
                            string input = item.tool_name;
                            Regex regex = new Regex(@"[0-9]+(\.[0-9]+)?");
                            MatchCollection matches = regex.Matches(input);
                            List<decimal> numbers = new List<decimal>();
                            foreach (Match match in matches)
                            {
                                if (decimal.TryParse(match.Value, out decimal result))
                                {
                                    // 将转换成功的数字添加到 List 中
                                    numbers.Add(result);
                                }
                            }

                            decimal width = numbers[0];
                            decimal r = numbers[1];

                            tool.knife_width = width;
                            tool.R = r;
                            tool.accuracy = item.accuracy;
                            tool.program_seq = (int?)int.Parse(item.sequence);
                            tool.work_order_status = work_Order.process_Status;
                            _repository.DbContext.Entry(tool).State = EntityState.Added;

                            EQU_TOOL eQU_TOOL = new EQU_TOOL();
                            eQU_TOOL.work_order_id = workOrderId;

                            eQU_TOOL.tool_name = item.tool_name;
                            eQU_TOOL.handle_type = item.tool_handle;
                            eQU_TOOL.collet_type = item.tool_collet;
                            //tool.R=
                            eQU_TOOL.knife_length = item.knife_length;
                            eQU_TOOL.blade_length = item.blade_length;
                            eQU_TOOL.category = item.category;
                            eQU_TOOL.handle_type = item.tool_handle;
                            eQU_TOOL.collet_type = item.tool_collet;
                            eQU_TOOL.reality_knife_width = 10000;
                            eQU_TOOL.reality_pairing_length = 10000;
                            eQU_TOOL.reality_R = 10000;
                            eQU_TOOL.vendor_code = item.vendor_code;
                            eQU_TOOL.tool_materials = item.tool_materials;                                           
                            eQU_TOOL.knife_width = width;
                            eQU_TOOL.R = r;
                            eQU_TOOL.time= item.time;
                            eQU_TOOL.accuracy = item.accuracy;
                            eQU_TOOL.program_seq = (int?)int.Parse(item.sequence);
                            eQU_TOOL.work_order_status = work_Order.process_Status;
                            eQU_TOOL.CreateBy = UserContext.Current.UserName;
                            eQU_TOOL.CreateOn = DateTime.Now;
                            eQU_TOOL.Identifier = proCodeMain;
                            eQU_TOOL.Ace1LTheory = item.Ace1LTheory;
                            eQU_TOOL.Ace1DTheory = item.Ace1DTheory;
                            eQU_TOOL.Ace2LTheory = item.Ace2LTheory;
                            eQU_TOOL.Ace2DTheory = item.Ace2DTheory;
                            eQU_TOOL.Ace3LTheory = item.Ace3LTheory;
                            eQU_TOOL.Ace3DTheory = item.Ace3DTheory;
                            eQU_TOOL.Holder_Code=item.Holder_Code;
                            _repository.DbContext.Entry(eQU_TOOL).State = EntityState.Added;

                            PROC_PROGRAM pROC_PROGRAM = new PROC_PROGRAM();
                            pROC_PROGRAM.ftpmachiningcenter_id = item.ftpmachiningcenter_id;
                            pROC_PROGRAM.sequence =int.Parse( item.sequence);
                            pROC_PROGRAM.program_name = item.program_name;
                            pROC_PROGRAM.Identifier = proCodeMain;
                            pROC_PROGRAM.tool_name = item.tool_name;
                            pROC_PROGRAM.tool_handle = item.tool_handle;
                            pROC_PROGRAM.tool_collet = item.tool_collet;
                            pROC_PROGRAM.knife_length = item.knife_length;
                            pROC_PROGRAM.blade_length = item.blade_length;
                            pROC_PROGRAM.category = item.category;
                            pROC_PROGRAM.accuracy = item.accuracy;
                            pROC_PROGRAM.rotate_speed = item.rotate_speed;
                            pROC_PROGRAM.feed = item.feed;
                            pROC_PROGRAM.side_bottom_margin = item.side_bottom_margin;
                            pROC_PROGRAM.z_depth = item.z_depth;
                            pROC_PROGRAM.tool_materials = item.tool_materials;
                            pROC_PROGRAM.vendor_code = item.vendor_code;
                            pROC_PROGRAM.remark = item.remark;
                            pROC_PROGRAM.time = item.time;
                            pROC_PROGRAM.CreateBy = UserContext.Current.UserName;
                            pROC_PROGRAM.CreateOn = DateTime.Now;
                            pROC_PROGRAM.ProCode= proCodeMain;
                            pROC_PROGRAM.Id=Guid.Parse( Guid.NewGuid().ToString().ToUpper());

                            _repository.DbContext.Entry(pROC_PROGRAM).State = EntityState.Added;

                            PROC_MATERIAL_TOOL pROC_MATERIAL_TOOL = new PROC_MATERIAL_TOOL();
                            pROC_MATERIAL_TOOL.MaterialId = pROC_MATERIAL.Id.ToString().ToUpper();
                            pROC_MATERIAL_TOOL.ToolId = eQU_TOOL.Id.ToString().ToUpper();
                            pROC_MATERIAL_TOOL.ProgramId = pROC_PROGRAM.Id.ToString().ToUpper();
                            pROC_MATERIAL_TOOL.CreateBy = UserContext.Current.UserName;
                            pROC_MATERIAL_TOOL.CreateOn = DateTime.Now;
                            _repository.DbContext.Entry(pROC_MATERIAL_TOOL).State = EntityState.Added;
                        }
                        _repository.SaveChanges();
                        transaction.Commit();

                        return 1;
                    }
                    catch(Exception ex)
                    {
                        transaction.Rollback();
                        return -1;
                    }
                }
            }
            else
            {

                //只添加item tool
                var workorder =_repository.DbContext.Set<upper_work_order>().Where(a => a.workpiece_id == workpiece.id).FirstOrDefault();
                var workorderid = workorder.work_order_id;
                upper_ftpmachiningcenter machiningcenter = _repository.DbContext.Set<upper_ftpmachiningcenter>().Where(a => a.work_order_id == workorderid).FirstOrDefault();
                var machiningprograms=_repository.DbContext.Set<upper_ftpprogram>().Where(a=>a.ftpmachiningcenter_id==machiningcenter.ftp_id).ToList();

                //更新刀具time by guoyuguo 2024.9.24
                PLAN_WORK_ORDER planWorkOrder = _repository.DbContext.Set<PLAN_WORK_ORDER>().Where(a => a.work_order_number == center.mold_number).FirstOrDefault();
                planWorkOrder.UsedToolTotal = programs.Count();
                _repository.DbContext.Entry(planWorkOrder).State = EntityState.Modified;
                foreach (var item in programs)
                {
                  var proseq=  (int?)int.Parse(item.sequence);
                    EQU_TOOL eQU_TOOL = _repository.DbContext.Set<EQU_TOOL>().Where(a => a.Identifier.StartsWith(planWorkOrder.OrderCode)&&a.program_seq== proseq).FirstOrDefault();
                    if(eQU_TOOL!=null)
                    {
                        eQU_TOOL.time = item.time;
                        _repository.DbContext.Entry(eQU_TOOL).State = EntityState.Modified;

                    }
                    var sequence= int.Parse(item.sequence);
                    PROC_PROGRAM pROC_PROGRAM = _repository.DbContext.Set<PROC_PROGRAM>().Where(a => a.Identifier.StartsWith(planWorkOrder.OrderCode) && a.sequence == sequence).FirstOrDefault();
                    if (pROC_PROGRAM != null)
                    {
                        pROC_PROGRAM.time = item.time;
                        _repository.DbContext.Entry(pROC_PROGRAM).State = EntityState.Modified;
                    }
                    if (machiningprograms.Any(a => a.program_name == item.program_name))
                    {
                        continue;
                    }
                    else
                    {
                        item.ftpmachiningcenter_id = machiningcenter.ftp_id;
                        //item.accuracy = item.category == "光" ? 0.03m : 0.1m;
                        _repository.DbContext.Entry(item).State = EntityState.Added;

                        //同时生成刀具
                        upper_tool tool = new upper_tool();
                        tool.work_order_id = workorderid;

                        tool.tool_name = item.tool_name;
                        tool.handle_type = item.tool_handle;
                        tool.collet_type = item.tool_collet;
                        //tool.R=
                        tool.knife_length = item.knife_length;
                        tool.blade_length = item.blade_length;
                        tool.category = item.category;
                        tool.handle_type = item.tool_handle;
                        tool.collet_type = item.tool_collet;
                        tool.reality_knife_width = 10000;
                        tool.reality_pairing_length = 10000;
                        tool.reality_R = 10000;
                        //tool.collet_length=
                        // tool.extensions_type=
                        //tool.extensions_length=
                        //tool.tool_type=
                        // tool.tool_length= tool.knife_length;
                        //tool.pairing_length=
                        // tool.blade_width= 
                        string input = item.tool_name;
                        Regex regex = new Regex(@"[0-9]+(\.[0-9]+)?");
                        MatchCollection matches = regex.Matches(input);
                        List<decimal> numbers = new List<decimal>();
                        foreach (Match match in matches)
                        {
                            if (decimal.TryParse(match.Value, out decimal result))
                            {
                                // 将转换成功的数字添加到 List 中
                                numbers.Add(result);
                            }
                        }

                        decimal width = numbers[0];
                        decimal r = numbers[1];

                        tool.knife_width = width;
                        tool.R = r;
                        tool.accuracy = item.accuracy;
                        tool.program_seq = (int?)int.Parse(item.sequence);
                        tool.work_order_status = workorder.process_Status;
                        //_repository.DbContext.Entry(tool).State = EntityState.Added;

                      
                        
                    }

                }
               return _repository.SaveChanges();
            }

              
        }


        public upper_work_order GetWorkOrderEntity(string workOrderId)
        {
            var result = int.TryParse(workOrderId, out var intParam);
            if (!result)
            {
                return null;
            }
            var workOrderEntity = _repository.DbContext.Set<upper_work_order>().Where(a => a.work_order_id == intParam).FirstOrDefault();
            if (workOrderEntity == null)
            {
                return null;
            }
            else
            {
                return workOrderEntity;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="workOrderId"></param>
        /// <param name="index">1打表,2分中,3放平</param>
        /// <returns></returns>
        public int ChangeText(string workOrderId,int index,string text)
        {

           var result = int.TryParse(workOrderId,out var intParam);
            if (!result)
            {
                return -1;
            }
            var workOrderEntity = _repository.DbContext.Set<upper_work_order>().Where(a => a.work_order_id == intParam).FirstOrDefault();
            if (workOrderEntity==null)
            {
                return -1;
            }
            

            switch(index)
            {
                case 1:
                    workOrderEntity.measure_number = text;
                    break;
                case 2:
                    workOrderEntity.centring_number = text;
                    break;
                case 3:
                    workOrderEntity.make_level_number = text;
                    break; 
                default:
                    break;
            }
            _repository.DbContext.Entry(workOrderEntity).State = EntityState.Modified;
            return _repository.SaveChanges();

        }


        public List<upper_file> GetDataFiles() 
        {

            var fileList = _repository.DbContext.Set<upper_file>().AsNoTracking().ToList();
            if (fileList == null)
            {
                return null;
            }
            else
            {
                return fileList;
            }
        }
  }
}
